/***************************************************************************//**
 * @file
 * @brief Core application logic.
 *******************************************************************************
 * # License
 * <b>Copyright 2022 Silicon Laboratories Inc. www.silabs.com</b>
 *******************************************************************************
 *
 * SPDX-License-Identifier: Zlib
 *
 * The licensor of this software is Silicon Laboratories Inc.
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 *
 ******************************************************************************/
#include "em_common.h"
#include "app_assert.h"
#include "app_log.h"
#include "sl_status.h"
#include "app.h"

#include "sl_btmesh_api.h"
#include "sl_bt_api.h"

//ZSM add
#include "sl_simple_timer.h"
#include "input_mesh.h"
#include "input_data_handle.h"
#include "cdn_nvm3.h"
#include "cdn_adv.h"
#include "cdn_light.h"
#include "cdn_pwm.h"
#include "gatt_db.h"
#include "cdn_key.h"
#include "em_wdog.h"
#include "cdn_power_switch_reset.h"

#define CDN_RELAY_ENABLE 1
/// Advertising Provisioning Bearer
#define PB_ADV                         0x1
/// GATT Provisioning Bearer
#define PB_GATT                        0x2
//zsm define
#define NO_CALLBACK_DATA              (void *)NULL
#define APP_CDN_LED_HANDLE_TIMEOUT    1       //1ms
#define POWER_SWITCH_MS_TIMEOUT    10       //10ms
#define APP_CDN_KEYSCAN_TIMEOUT       20      //20ms
#define ENTER_CDN_GROUP_FAIL_TIMEOUT  1000    //1000ms
#define CDN_PWM_LOOP_TIMEOUT          1       //1ms
#define FACTORY_TEST_TIMEOUT          100

extern LightInfo CdnLightNode;

//zsm variable
static sl_simple_timer_t CDN_10msTimer;
static sl_simple_timer_t CDN_LedTimer;
static sl_simple_timer_t CDN_KeyTimer;
static sl_simple_timer_t CDN_EnterGroupTimer;
static sl_simple_timer_t CDN_PwmLightnessTimer;
static sl_simple_timer_t CDN_PwmCTTimer;
static sl_simple_timer_t CDN_FactoryTimer;



//蓝牙连接句柄
static uint8_t ConnectHandle;
//gatt连接标志
static uint8_t gatt_opened;

void SetConnectHandle(uint8_t flag)
{
	ConnectHandle = flag;
}

uint8_t GetConnectHandle(void)
{
	return ConnectHandle;
}

void SetGattConnectionOpened(uint8_t flag)
{
	gatt_opened = flag;
}

uint8_t GetGattConnectionOpened(void)
{
	return gatt_opened;
}

//看门狗初始化
void WDOGInit(void)
{
  // Enable clock for the WDOG module; has no effect on xG21
  CMU_ClockEnable(cmuClock_WDOG0, true);

  // Watchdog Initialize settings 
  WDOG_Init_TypeDef wdogInit = WDOG_INIT_DEFAULT;
  CMU_ClockSelectSet(cmuClock_WDOG0, cmuSelect_ULFRCO); /* ULFRCO as clock source */
  wdogInit.debugRun = true;
  wdogInit.em3Run = true;
  wdogInit.perSel = wdogPeriod_16k; // 16K大约15秒复位,4097 clock cycles of a 1kHz clock  ~4 seconds period

  // Initializing watchdog with chosen settings 
  WDOGn_Init(DEFAULT_WDOG, &wdogInit);
}


static void AppLogHealthServer(void)
{
	static uint8_t minute;

	static uint32_t stamp = 0;
	minute ++;

	if(minute > 60){	//1 minute
		minute = 0;
		stamp = stamp + 1 >= 0xfffffffeL ?
				0 : stamp + 1;
		TRACE("app log 1 minute stamp = 0x%lX\r\n", stamp);
	}
}
/*******************************************************************************
 * CDN_LedTimer Callbacks
 ******************************************************************************/
static void CDN_LedTimer_cb(sl_simple_timer_t *handle, void *data)
{
  (void)data;
  (void)handle;
  CdnLedStateHandle();
	CDN_Light_effect_display();
#if SCENE_POWER_OFF_MEMORY
	ExecuteMemorySceneId();
#endif
#if CDN_Lightness_ENABLE
  LightnessENHandle();
#endif
}
/*******************************************************************************
 * CDN_KeyTimer Callbacks
 ******************************************************************************/
static void CDN_KeyTimer_cb(sl_simple_timer_t *handle, void *data)
{
  (void)data;
  (void)handle;
  KeyGetValuePutEventBuff();
	KeyFactoryResetControlHandle();
	KeyFactoryTestInto();
}
/*******************************************************************************
 * CDN_EnterGroupTimer_cb Callbacks
 ******************************************************************************/
static void CDN_EnterGroupTimer_cb(sl_simple_timer_t *handle, void *data)
{
  (void)data;
  (void)handle;
  AppLogHealthServer();
  FailedToEnterNetworkGroup();
}
/*******************************************************************************
 * CDN_PwmLightnessTimer_cb Callbacks
 ******************************************************************************/
static void CDN_PwmLightnessTimer_cb(sl_simple_timer_t *handle, void *data)
{
  (void)data;
  (void)handle;
#if CDN_LIGHT_BULB
  CdnLightnessProcessActionForBulb();
#elif CDN_LIGHT_STRIP
  CdnLightnessProcessActionForStrip();
#endif
}
/*******************************************************************************
 * CDN_PwmCTTimer_cb Callbacks
 ******************************************************************************/
static void CDN_PwmCTTimer_cb(sl_simple_timer_t *handle, void *data)
{
  (void)data;
  (void)handle;
#if CDN_LIGHT_BULB
  CdnColortempProcessActionForBulb();
#elif CDN_LIGHT_STRIP
  CdnColortempProcessActionForStrip();
#endif
}
/*******************************************************************************
 * CDN_FactoryTimer_cb Callbacks
 ******************************************************************************/
static void CDN_FactoryTimer_cb(sl_simple_timer_t *handle, void *data)
{
  (void)data;
  (void)handle;
  CdnFactoryTestStep();
}

/*******************************************************************************
 * CDN_10msTimer_cb Callbacks
 ******************************************************************************/
static void CDN_10msTimer_cb(sl_simple_timer_t *handle, void *data)
{
  (void)data;
  (void)handle;
#if POWER_SWITCH_RESET
  PowerSwitchHandle();
  CDN_Power_Switch_display();
#endif
}

/*******************************************************************************
 * Application CDN_10msTimerInit.
 *****************************************************************************/
static void CDN_10msTimerInit(void)
{
  sl_status_t sc = sl_simple_timer_start(&CDN_10msTimer,
                                         POWER_SWITCH_MS_TIMEOUT,
                                         CDN_10msTimer_cb,
                                         NO_CALLBACK_DATA,
                                         true);
  TRACE("1msTimer init !!\r\n");
  app_assert_status_f(sc, "Failed to start 1msTimer  periodic timer\n");
}

/*******************************************************************************
 * Application CDN_LedTimerInit.
 *****************************************************************************/
static void CDN_LedTimerInit(void)
{
  sl_status_t sc = sl_simple_timer_start(&CDN_LedTimer,
                                         APP_CDN_LED_HANDLE_TIMEOUT,
                                         CDN_LedTimer_cb,
                                         NO_CALLBACK_DATA,
                                         true);
  TRACE("zsm: CDN_LedTimerInit Light\r\n");
  app_assert_status_f(sc, "Failed to start CDN_LedTimerInit periodic timer\n");
}
/*******************************************************************************
 * Application CDN_KeyTimerInit.
 *****************************************************************************/
static void CDN_KeyTimerInit(void)
{
  sl_status_t sc = sl_simple_timer_start(&CDN_KeyTimer,
                                         APP_CDN_KEYSCAN_TIMEOUT,
                                         CDN_KeyTimer_cb,
                                         NO_CALLBACK_DATA,
                                         true);
  TRACE("zsm: CDN_KeyTimerInit\r\n");
  app_assert_status_f(sc, "Failed to start CDN_KeyTimerInit periodic timer\n");
}
/*******************************************************************************
 * Application CDN_EnterGroupTimerInit.
 *****************************************************************************/
static void CDN_EnterGroupTimerInit(void)
{
  sl_status_t sc = sl_simple_timer_start(&CDN_EnterGroupTimer,
                                         ENTER_CDN_GROUP_FAIL_TIMEOUT,
                                         CDN_EnterGroupTimer_cb,
                                         NO_CALLBACK_DATA,
                                         true);
  TRACE("zsm: CDN_EnterGroupTimerInit Light\r\n");
  app_assert_status_f(sc, "Failed to start CDN_EnterGroupTimerInit periodic timer\n");
}
/*******************************************************************************
 * Application CDN_PwmLightnessTimerInit.
 *****************************************************************************/
static void CDN_PwmLightnessTimerInit(void)
{
  sl_status_t sc = sl_simple_timer_start(&CDN_PwmLightnessTimer,
                                         CDN_PWM_LOOP_TIMEOUT,
                                         CDN_PwmLightnessTimer_cb,
                                         NO_CALLBACK_DATA,
                                         true);
  TRACE("zsm: CDN_PwmLoopTimerInit Light\r\n");
  app_assert_status_f(sc, "Failed to start CDN_PwmLoopTimerInit periodic timer\n");
}
/*******************************************************************************
 * Application CDN_PwmCTTimerInit.
 *****************************************************************************/
static void CDN_PwmCTTimerInit(void)
{
  sl_status_t sc = sl_simple_timer_start(&CDN_PwmCTTimer,
                                         CDN_PWM_LOOP_TIMEOUT,
                                         CDN_PwmCTTimer_cb,
                                         NO_CALLBACK_DATA,
                                         true);
  TRACE("zx: CDN_PwmCTTimerInit Light\r\n");
  app_assert_status_f(sc, "Failed to start CDN_PwmCTTimerInit periodic timer\n");
}
/*******************************************************************************
 * Application CDN_FactoryTimerInit.
 *****************************************************************************/
static void CDN_FactoryTimerInit(void)
{
  sl_status_t sc = sl_simple_timer_start(&CDN_FactoryTimer,
                                         FACTORY_TEST_TIMEOUT,
                                         CDN_FactoryTimer_cb,
                                         NO_CALLBACK_DATA,
                                         true);
  TRACE("zsm: sl_simple_timer_start factory\r\n");
  app_assert_status_f(sc, "Failed to start CDN_FactoryTimer periodic timer\n");
}
/**************************************************************************//**
 * Application CDN_Judge_Remain_Seqnum_Is_Enough.
 *****************************************************************************/
static void CDN_judge_remain_seqnum_is_enough(void)
{
	uint32_t remain_seq;
	sl_status_t sc;
	sc = sl_btmesh_node_get_seq_remaining(0, &remain_seq);
	if(sc == SL_STATUS_OK)
	{
		TRACE("zx: CDN_Judge_Remain_Seqnum_Is_Enough 0x%lx\n",remain_seq);
	}
	else
  {
    TRACE("zx: CDN_Judge_Remain_Seqnum_Is_Enough Faile 0x%lx\n",sc);
  }
	if(remain_seq <= 0x5ffff)
	{
		sc = sl_btmesh_test_set_element_seqnum(0, 0);
		TRACE("zx: Reset SEQ number %s\n",sc == SL_STATUS_OK ? "SUCCESS" : "FAILED");
	}
}
/*******************************************************************************
 * Application Init.
 *****************************************************************************/
SL_WEAK void app_init(void)
{
  /////////////////////////////////////////////////////////////////////////////
  // Put your additional application init code here!                         //
  // This is called once during start-up.                                    //
  /////////////////////////////////////////////////////////////////////////////
  cdn_nvm3_init();
	cdn_sl_pwm_init_instances(); //PWM客制初始化，适配筒灯和灯 
	Light_GPIO_Init();
  CDN_LedTimerInit();
	CDN_KeyTimerInit();
  CDN_EnterGroupTimerInit();
  CDN_PwmLightnessTimerInit();
  CDN_PwmCTTimerInit();
  CDN_FactoryTimerInit();
  ReadLightInfoFromFlash();
#if CDN_LIGHT_BULB
  LightPowerWorkModeInit();//功率模式设置
#endif
	WDOGInit();
	CdnDimPwmStart();
	CdnCtPwmStart();
#if SCENE_POWER_OFF_MEMORY
	LightSceneMemoryInit();
#endif
	CDN_10msTimerInit();
}

/*******************************************************************************
 * Application Process Action.
 *****************************************************************************/
SL_WEAK void app_process_action(void)
{
  /////////////////////////////////////////////////////////////////////////////
  // Put your additional application code here!                              //
  // This is called infinitely.                                              //
  // Do not call blocking functions from here!                               //
  /////////////////////////////////////////////////////////////////////////////
	WDOGn_Feed(DEFAULT_WDOG);
	InputEventProcess();
  CheckLightInfoIfChange();
#if SCENE_POWER_OFF_MEMORY
  CheckLightSceneInfoIfChange();
#endif
  cdn_nvm3_process_action();
}

/*******************************************************************************
 * Bluetooth stack event handler.
 * This overrides the dummy weak implementation.
 *
 * @param[in] evt Event coming from the Bluetooth stack.
 *****************************************************************************/
void sl_bt_on_event(struct sl_bt_msg *evt)
{
  sl_status_t sc;
  switch (SL_BT_MSG_ID(evt->header)) {
    case sl_bt_evt_system_boot_id:
      {
        // Initialize Mesh stack in Node operation mode,
        // wait for initialized event
        TRACE("zsm: Light Node init\r\n");
        sc = sl_btmesh_node_init();
        app_assert_status_f(sc, "zsm: Failed to init Light node \r\n");
      }
      break;
    ///////////////////////////////////////////////////////////////////////////
    // Add additional event handlers here as your application requires!      //
    ///////////////////////////////////////////////////////////////////////////
    case sl_bt_evt_scanner_scan_report_id:
      {
        if(evt->data.evt_scanner_scan_report.packet_type == 0 && !GetProvisionFlag()) //未入网
        {
          user_adv_filter_proc(evt);
        }
      }
      break;
    case sl_bt_evt_connection_opened_id:
	{
      sc = sl_bt_connection_set_parameters(evt->data.evt_connection_opened.connection,
                                           6,
                                           32,
                                           0,
                                           500,
                                           0,
                                           0);
      if(sc == SL_STATUS_OK)
      {
				if(GetProvisionFlag() == 0)
				{
					cdn_adv_stop();	//关闭广播
				}
        TRACE("zsm: Light sl_bt_connection_set_parameters Success !\r\n");
      }
      else
      {
        TRACE("zsm: Light sl_bt_connection_set_parameters Fail = 0x%lx !\r\n",sc);
      }
			SetGattConnectionOpened(true);
			SetConnectHandle(evt->data.evt_connection_opened.connection);
      TRACE("zsm: sl_bt_evt_connection_opened_id = %d\r\n",evt->data.evt_connection_opened.connection);
    	}
      break;
    case sl_bt_evt_connection_closed_id:   
			if(GetProvisionFlag() == 0)
			{
				cdn_adv_start();	//打开广播
			}
			SetGattConnectionOpened(false);
      TRACE("zsm: sl_bt_evt_connection_closed_id !\r\n");
      break;
    case sl_bt_evt_gatt_server_attribute_value_id:
      {
				TRACE("sl_bt_evt_gatt_server_attribute_value_id\n");
        if(evt->data.evt_gatt_server_attribute_value.attribute == gattdb_TOOL2BT)
        {
					#define VALUE_LEN 							5
					#define GATT_RESET_CMD 					0x64
					#define GATT_RESET_DATA 				0x01
					#define GATT_FACTORY_CMD 				0x65
					#define GATT_FACTORY_START_DATA 0x01
					#define GATT_FACTORY_STOP_DATA  0x02
					
					uint8_t pValue[VALUE_LEN] = {0};
          uint8_t pValueLen = evt->data.evt_gatt_server_attribute_value.value.len;
          if(pValueLen > VALUE_LEN)
          {
            pValueLen = VALUE_LEN;
          }
          memcpy(pValue,evt->data.evt_gatt_server_attribute_value.value.data,pValueLen);

		  uint8_t company_id = pValue[0];
		  uint8_t device_pid = pValue[1];
		  uint8_t device_sid = pValue[2];
		  uint8_t gatt_cmd   = pValue[3];
		  uint8_t gatt_data  = pValue[4];
      TRACE("lj: company_id = 0x%2x, gatt_cmd = 0x%2x,gatt_data = 0x%2x\n",company_id,gatt_cmd,gatt_data);
      TRACE("lj: Device_pid = 0x%2x,Device_sid = 0x%2x\n",device_pid,device_sid);
		  if(0xA0 == company_id && (is_valid_device(device_pid, device_sid) == SUCCESS))
		  {
			switch(gatt_cmd)
			{
				case GATT_RESET_CMD:
				{
					if(GATT_RESET_DATA == gatt_data)
					{
						sl_btmesh_initiate_node_reset();  
	            		TRACE("evt:gatt write bt reset OK\r\n");
                        //SetCurrentLightPowerWorkMode(Power_default_Mode);
								}
								break;
							}
							case GATT_FACTORY_CMD:
							{
								if(!GetProvisionFlag()) //为入网状态下才可以进行产测模 
								{
									if(GATT_FACTORY_START_DATA == gatt_data)
									{
										CdnStartFactoryTest();
										TRACE("evt:gatt write bt start factory OK\r\n");
									}
									else if(GATT_FACTORY_STOP_DATA == gatt_data)
									{
										CdnStopFactoryTest();
										TRACE("evt:gatt write bt stop factory OK\r\n");
									}
								}
							}
							default:
								break;
						}
					}
        }
      }
      break;
    // -------------------------------
    // Default event handler.
    default:
      break;
  }
}

/*******************************************************************************
 * Bluetooth Mesh stack event handler.
 * This overrides the dummy weak implementation.
 *
 * @param[in] evt Event coming from the Bluetooth Mesh stack.
 *****************************************************************************/
void sl_btmesh_on_event(sl_btmesh_msg_t *evt)
{
  sl_status_t sc;
  switch (SL_BT_MSG_ID(evt->header)) {
    case sl_btmesh_evt_node_initialized_id:
      {
				CDN_judge_remain_seqnum_is_enough();
        SetProvisionFlag(evt->data.evt_node_initialized.provisioned);
        TRACE("zsm: Light sl_btmesh_on_event provisioned = %d\r\n",GetProvisionFlag());

        if(!GetProvisionFlag())
          {
            SetCurrentLightPowerWorkMode(Power_default_Mode);
          }

				//有入网但是没分组
				if(CdnLightNode.group_adr == 0 && evt->data.evt_node_initialized.provisioned)
				{
					sl_btmesh_initiate_node_reset();  //清除信息并重启
				}
        //未入网
        if (!evt->data.evt_node_initialized.provisioned)
        {
          // The Node is now initialized,
          // start unprovisioned Beaconing using PB-ADV and PB-GATT Bearers
          TRACE("zsm: Initialized\r\n");
          //sc = sl_btmesh_node_start_unprov_beaconing(PB_ADV | PB_GATT);
					sc = sl_btmesh_node_start_unprov_beaconing(PB_GATT);
          app_assert_status_f(sc, "Failed to start unprovisioned beaconing\n");
					
          mesh_adv_create_init(); //创建广播句柄
          mesh_user_adv_init();   //初始化广播数 
          mesh_scan_rsp_init();   //初始化广播回应数 
          cdn_adv_start();        //启动广播

          SetLedState(CDN_LED_NO_NET,0,0);
				}
        else
        {
          //已入网
          //初始化Vendor Model
          uint8_t VendorModel_op[1] = {CDN_VENDOR_OP_SET};
          sc = sl_btmesh_vendor_model_init(0,0x0211, 0,0,1,VendorModel_op);
          if(sc == SL_STATUS_OK)
          {
            TRACE("zsm: Light sl_btmesh_evt_node_initialized_id vendor_model_init Success !\r\n");
          }
          else
          {
            TRACE("zsm: Light sl_btmesh_evt_node_initialized_id vendor_model_init Fail !\r\n");
          }
          //配置代理
          uint8_t set_proxy_value = 1;
          uint8_t get_proxy_value = 0;
          sc = sl_btmesh_test_set_gatt_proxy(set_proxy_value,&get_proxy_value);
          if(sc==SL_STATUS_OK)
          {
             TRACE("zsm: sl_btmesh_evt_node_initialized_id gatt_proxy config setting OK\r\n");
          }
          else
          {
             TRACE("zsm: sl_btmesh_evt_node_initialized_id gatt_proxy config setting fail %ld \r\n",sc);
          }
          //设置重传次数
          sc = sl_btmesh_test_set_nettx(7,9);
          if(sc == SL_STATUS_OK)
          {
            TRACE("zsm: Light sl_btmesh_evt_node_initialized_id retransmission Init Success !\r\n");
          }
          else
          {
            TRACE("zsm: Light sl_btmesh_evt_node_initialized_id retransmission Init Fail = 0x%lx !\r\n",sc);
          }
          //配置中继
#if CDN_RELAY_ENABLE
          uint8_t set_relay_enable = 1;
          sc = sl_btmesh_test_set_relay(set_relay_enable,
                                        5,
                                        9);
          if(sc==SL_STATUS_OK)
          {
             TRACE("zsm: sl_btmesh_evt_node_initialized_id set_relay config setting OK\r\n");
          }
          else
          {
             TRACE("zsm: sl_btmesh_evt_node_initialized_id set_relay config setting fail %ld \r\n",sc);
          }
#endif
					//配置朋友
					uint8_t set_friend_enable = 1;
					uint8_t get_friend = 1;
					sc = sl_btmesh_test_set_friend(set_friend_enable, &get_friend);
					if(sc==SL_STATUS_OK)
					{
						 TRACE("zx: sl_btmesh_evt_node_provisioned_id set_friend config setting OK %d\r\n", get_friend);
					}
					else
					{
						 TRACE("zx: sl_btmesh_evt_node_provisioned_id set_friend config setting fail %ld %d\r\n",sc, get_friend);
					}
          //配置identity
          uint16_t netkey_index = 0;
          uint8_t set_identity_value = 0;
          sc = sl_btmesh_test_set_identity(0,1,&netkey_index,&set_identity_value);
          if(sc==SL_STATUS_OK)
          {
            TRACE("zsm: sl_btmesh_evt_node_initialized_id indentity setting OK\r\n");
          }else{
            TRACE("zsm: sl_btmesh_evt_node_initialized_id indentity setting fail %ld \r\n",sc);
          }
          SetLedState(CDN_LED_IDLE,0,0);

					cdn_get_node_information();
        }
      }
      break;
		case sl_btmesh_evt_node_key_added_id: //add network key
			{
				TRACE("sl_btmesh_evt_node_key_added:\r\n");
				TRACE("type 				= 0x%X\r\n", evt->data.evt_node_key_added.type);
				TRACE("index				= 0x%X\r\n", evt->data.evt_node_key_added.index);
				TRACE("netkey_index = 0x%X\r\n", evt->data.evt_node_key_added.netkey_index);
#if 1
				if(evt->data.evt_node_key_added.type == sl_btmesh_test_key_type_net)		//bind appkey
				{
					uint8_t appkey[] = {0x63, 0x96, 0x47, 0x71, 0x73, 0x4f, 0xbd, 0x76, 0xe3, 0xb4, 0x05, 0x19, 0xd1, 0xd9, 0x4a, 0x48};
					sc = sl_btmesh_test_add_local_key(sl_btmesh_test_key_type_app, *(aes_key_128*) appkey, 0, 0);
				 	if(!sc) {TRACE("sl_btmesh_test_add_local_key!\r\n");}
				 	app_assert_status_f(sc, "Failed to sl_btmesh_test_key_type_app\n"); 
					sc = sl_btmesh_test_bind_local_model_app(0, 0, 0x0211, 0);
					app_assert_status_f(sc, "Failed to sl_btmesh_test_key_type_app\n");
					if(!sc) {TRACE("sl_btmesh_test_bind_local_model_app!\r\n");}
				}
#endif
			}
			break;

    ///////////////////////////////////////////////////////////////////////////
    // Add additional event handlers here as your application requires!      //
    ///////////////////////////////////////////////////////////////////////////
		case sl_btmesh_evt_node_provisioning_started_id:
			{
				EnteringGroup();
				TRACE("zsm: sl_btmesh_evt_node_provisioning_started_id !\r\n");
			}
			break;

		case sl_btmesh_evt_node_provisioned_id:
      {
        //初始化Vendor Model
        uint8_t VendorModel_op[1] = {CDN_VENDOR_OP_SET};
        sc = sl_btmesh_vendor_model_init(0,0x0211, 0,0,1,VendorModel_op);
        if(sc == SL_STATUS_OK)
        {
            TRACE("zsm: Light sl_btmesh_evt_node_provisioned_id vendor_model_init Success !\r\n");
        }
        else
        {
            TRACE("zsm: Light sl_btmesh_evt_node_provisioned_id vendor_model_init Fail = 0x%lx !\r\n",sc);
        }
        //配置代理
        uint8_t set_proxy_value = 1;
        uint8_t get_proxy_value = 0;
        sc = sl_btmesh_test_set_gatt_proxy(set_proxy_value,&get_proxy_value);
        if(sc==SL_STATUS_OK)
        {
           TRACE("zsm: sl_btmesh_evt_node_provisioned_id gatt_proxy config setting OK\r\n");
        }
        else
        {
           TRACE("zsm: sl_btmesh_evt_node_provisioned_id gatt_proxy config setting fail %ld \r\n",sc);
        }
        //设置重传次数
        sc = sl_btmesh_test_set_nettx(7,9);
        if(sc == SL_STATUS_OK)
        {
          TRACE("zsm: Light sl_btmesh_evt_node_provisioned_id retransmission Success !\r\n");
        }
        else
        {
          TRACE("zsm: Light sl_btmesh_evt_node_provisioned_id retransmission Fail = 0x%lx !\r\n",sc);
        }
        //配置中继
#if CDN_RELAY_ENABLE
        uint8_t set_relay_enable = 1;
        sc = sl_btmesh_test_set_relay(set_relay_enable,
                                      5,
                                      9);
        if(sc==SL_STATUS_OK)
        {
           TRACE("zsm: sl_btmesh_evt_node_provisioned_id set_relay config setting OK\r\n");
        }
        else
        {
           TRACE("zsm: sl_btmesh_evt_node_provisioned_id set_relay config setting fail %ld \r\n",sc);
        }
#endif
				//配置朋友
        uint8_t set_friend_enable = 1;
        uint8_t get_friend = 1;
        sc = sl_btmesh_test_set_friend(set_friend_enable, &get_friend);
        if(sc==SL_STATUS_OK)
        {
           TRACE("zx: sl_btmesh_evt_node_provisioned_id set_friend config setting OK %d\r\n", get_friend);
        }
        else
        {
           TRACE("zx: sl_btmesh_evt_node_provisioned_id set_friend config setting fail %ld %d\r\n",sc, get_friend);
        }
        //配置identity
        uint16_t netkey_index = 0;
        uint8_t set_identity_value = 0;
        sc = sl_btmesh_test_set_identity(0,1,&netkey_index,&set_identity_value);
        if(sc==SL_STATUS_OK)
        {
          TRACE("zsm: sl_btmesh_evt_node_provisioned_id indentity setting OK\r\n");
        }else{
          TRACE("zsm: sl_btmesh_evt_node_provisioned_id indentity setting fail %ld \r\n",sc);
        }
        //user_adv_update();      //更新广播数据，更新服 0x1827->0x1828
        //user_scanRsp_update();  //更新回应包数 
        cdn_adv_stop();	//关闭广播
        TRACE("zsm: provisioned finish Light !\r\n");
      }
      break;
    case sl_btmesh_evt_vendor_model_receive_id:
      {
        TRACE("zsm: sl_btmesh_evt_vendor_model_receive_id Light--------- !\r\n");
        mesh_vendor_data_get(evt);
      }
      break;
    case sl_btmesh_evt_node_model_config_changed_id:  //app key 󶨳ɹ 
      TRACE("zsm: sl_btmesh_evt_node_model_config_changed_id model id = 0x%x\r\n",evt->data.evt_vendor_model_receive.model_id);
      break;
    case sl_btmesh_evt_proxy_connected_id:            //
      TRACE("zsm: sl_btmesh_evt_proxy_connected_id model id = 0x%x\r\n",evt->data.evt_vendor_model_receive.model_id);
      break;
    case sl_btmesh_evt_proxy_disconnected_id:         //
      TRACE("zsm: sl_btmesh_evt_proxy_disconnected_id !\r\n");
      break;
    // -------------------------------
    // Default event handler.
    default:
      break;
  }
}
